Page({
  data: {
    userType: 0,
    account_status: 'normal',
    userInfo: '',
    loginSuccess: false,

    //当前窗口的高度
    winHeight: 0,
    firstLogin: false,
    isLogin: false,
    loginError: false,
    located: true,

    isUpLoadLocation: true,
  },
  onLoad: function () {
    var that = this;
    var appInstance = getApp();

    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winHeight: res.windowHeight
        });
      }
    });


    //由于此界面不是程序打开之后直接加载的界面，因此在执行登录方法之前先判断是否已经登录过
    if (appInstance.globalData.hasLogin) {
      var userType1 = wx.getStorageSync('userType');
      var json = wx.getStorageSync('userInfo');

      if (userType1 && userType1 != 5) {
        var endTime = json.EfTime;

        var days = getDateDiff(endTime);
        console.log('days', days);
        if (days < 180 && days >= 90) {
          this.setData({
            account_status: 'warning'
          });
        } else if (days < 90) {
          this.setData({
            account_status: 'danger'
          });
        } else {
          this.setData({
            account_status: 'normal'
          });
        }
      }

      this.setData({
        userType: userType1,
        userInfo: json,
        loginSuccess: true
      });
    } else {
      login(this, appInstance);
    }



  },
  onReady: function () {
    // 页面渲染完成
    console.log('onReady123', 'onReady');
  },
  onShow: function (options) {
    // 页面显示
    var appInstance = getApp();
    if (appInstance.globalData.hasLogin) {
      if (this.data.firstLogin) {
        var userType1 = wx.getStorageSync('userType');
        var json = wx.getStorageSync('userInfo');

        if (userType1 && userType1 != 5) {
          var endTime = json.EfTime;

          var days = getDateDiff(endTime);
          console.log('days', days);
          if (days < 180 && days >= 90) {
            this.setData({
              account_status: 'warning'
            });
          } else if (days < 90) {
            this.setData({
              account_status: 'danger'
            });
          } else {
            this.setData({
              account_status: 'normal'
            });
          }
        }

        this.setData({
          userType: userType1,
          userInfo: json
        });
      }

      this.setData({
        firstLogin: false,
        isLogin: false,
        loginError: false,
        loginSuccess: true
      });
    } else if (appInstance.globalData.hasLogout) {
      this.setData({
        firstLogin: true,
        isLogin: false,
        loginError: false,
      });
    }

    //实时定位用的
    var isUpLoadLocation = getApp().globalData.isUpLoadLocation
    console.log("isuploadlocation" + isUpLoadLocation)
    this.setData({
      isUpLoadLocation: isUpLoadLocation
    })
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },

  userHeadClick: function () {
    wx.navigateTo({
      url: '../../login/login',
    })
  },

  bindLogout: function () {
    var that = this;
    var appInstance = getApp();

    wx.showModal({
      content: "您确定注销当前用户吗？",
      confirmText: "确定",
      cancelText: "取消",
      success: function (res) {
        if (res.confirm) {
          that.setData({
            isLogin: false
          });

          appInstance.globalData.hasLogin = false;
          appInstance.globalData.hasLogout = true;
          wx.clearStorageSync();

          that.setData({
            firstLogin: true,
            isLogin: false,
            loginError: false,
          });

          wx.navigateTo({
            url: '../../login/login',
          })
        }
      }
    });
    console.log('logout');
  },

  goLogin() {
    wx.navigateTo({
      url: '../../login/login',
    })
  },

  onShareAppMessage: function () {

  },

  openCloseLocation: function (e) {
    var userInfo = wx.getStorageSync('userInfo');
    console.log(userInfo)
    var appInstance = getApp();
    var status;
    if (e.detail.value) {
      // status = 1;
      // this.setData({
      //   located: true
      // });
      // appInstance.startLocationListener();
      console.log(getApp().globalData.isUpLoadLocation)
      console.log(e.detail.value)
      getApp().globalData.isUpLoadLocation = e.detail.value
      console.log(getApp().globalData.isUpLoadLocation)
    } else {
      // status = 0;
      // appInstance.stopLocationListener();
      console.log(getApp().globalData.isUpLoadLocation)
      console.log(e.detail.value)

      getApp().globalData.isUpLoadLocation = e.detail.value
      console.log(getApp().globalData.isUpLoadLocation)
    }
    // var that = this;
    // wx.showToast({
    //   title: '正在加载...',
    //   icon: 'loading',
    //   duration: 10000
    // });

    wx.showToast({
      title: '操作成功',
      icon: 'success',
      duration: 2000
    });

    // var id;
    // switch (this.data.userType) {
    //   case '1':

    //     break;

    //   case '2':

    //     break;

    //   case '3':
    //     id = userInfo.Id;
    //     break;

    //   case '4':
    //     id = userInfo.Id;
    //     break;

    //   case '5':
    //     id = userInfo.Id;
    //     break;
    // }

    // wx.request({
    //   url: 'https://www.lanshuicar.com/Server/GpsOpen.ashx',
    //   data: {
    //     id: id,
    //     type: that.data.userType,
    //     open: status
    //   },
    //   method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    //   // header: {}, // 设置请求的 header
    //   success: function (res) {
    //     // success
    //     var code = res.data.code;
    //     console.log(res);
    //     if (code == 0) {
    //       wx.showToast({
    //         title: res.data.msg,
    //         icon: 'success',
    //         duration: 2000
    //       });
    //     }
    //   },
    //   fail: function () {
    //     // fail
    //     wx.hideToast();
    //   },
    //   complete: function () {
    //     // complete

    //   }
    // });
  },


  callphone: function () {
    wx.makePhoneCall({
      phoneNumber: '0512-62881403' //仅为示例，并非真实的电话号码
    });
  },

  // locationtest: function () {
  //   wx.getLocation({
  //     type: 'gcj02',
  //     success: function (res) {
  //       console.log(res);

  //       // that.setData({
  //       //   latitude: res.latitude,
  //       //   longitude: res.longitude,
  //       // });

  //       // that.data.markers[0].latitude = res.latitude
  //       // that.data.markers[0].longitude = res.longitude
  //       // that.data.speed = res.speed
  //       // that.data.accuracy = res.accuracy
  //     }
  //   })
  // }
})

function getDateDiff(startDate) {
  var startTime = new Date(
    Date.parse(startDate.replace(/-/g, "/"))).getTime();

  var endTime = new Date().getTime();

  var dates = (startTime - endTime) / (1000 * 60 * 60 * 24);
  return dates;
}

function login(that, appInstance) {

  //在在App打开时，从存储中取出username和password，并执行登录方法，登录成功，就将全局变量中的hasLogin置为true，否则为false

  var username = wx.getStorageSync('username');
  var password = wx.getStorageSync('password');
  console.log('app.js', 'login start');
  console.log('app.js', username);
  if (username && password) {
    that.setData({
      firstLogin: false,
      isLogin: true,
      loginError: false
    });

    wx.request({
      url: 'https://www.lanshuicar.com/Server/LoginAPI.ashx',
      data: {
        Tel: username,
        password: password
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        // success
        wx.hideToast();
        console.log('app.js', res);
        var code = res.data.code;
        if (code == 0) {
          console.log('app.js', 'login success');
          appInstance.globalData.hasLogin = true;

          var json = res.data.data[0];
          wx.setStorageSync('userInfo', json);

          that.setData({
            firstLogin: false,
            isLogin: false,
            loginError: false
          });

          

        } else {
          console.log('app.js', 'login error');
          appInstance.globalData.hasLogin = false;

          that.setData({
            firstLogin: false,
            isLogin: false,
            loginError: true
          });

          appInstance.stopLocationListener();
        }
      },
      fail: function () {
        // fail
        console.log('app.js', 'login error');
        appInstance.globalData.hasLogin = false;

        that.setData({
          firstLogin: false,
          isLogin: false,
          loginError: true
        });

        appInstance.stopLocationListener();
      },
      complete: function () {
        wx.hideToast();
      }
    })
  } else {
    console.log('app.js', 'login error');
    appInstance.globalData.hasLogin = false;

    that.setData({
      firstLogin: true,
      isLogin: false,
      loginError: false
    });
  }
}